var JiguiNode = function (_element,_data) {
	
	// --------------------------------------------------------------------------
    // import
    // --------------------------------------------------------------------------
    
    const Plugin = ghca_charts.view.elements.plugins.plugin;
    const ImageElement = ghca_charts.view.elements.imageElement;
    const ObjectUtil = ghca_charts.view.util.objectUtil;
    const RegexUtil = ghca_charts.view.util.regexUtil;
    const RectElement = ghca_charts.view.elements.rectElement;
    Plugin.apply(this,arguments);
    var self = this,
        imageElement = _element,
        backgroundEle;

    self.config({
    	imageWidth:32,
        imageHeight:32,
        opacity:1
    });

    //override
    this.nameSpace = function(){
        return "jiguiNode";
    }
    //override
    this.update = function(g){
    	if(!g) {
    		g = this.el;
    	}
    	if(imageElement) {
            var config = ObjectUtil.cloneObj(self.config());
            config.color = RegexUtil.replace(RegexUtil.dataRegex,config.color,self.data());
            config.url = RegexUtil.replace(RegexUtil.dataRegex,config.url,self.data());
            config.imageWidth = RegexUtil.replace(RegexUtil.dataRegex,config.imageWidth,self.data());
            config.imageHeight = RegexUtil.replace(RegexUtil.dataRegex,config.imageHeight,self.data());
    		imageElement.data(config);
    	}
    	var title = RegexUtil.replace(RegexUtil.dataRegex,config.title,self.data());
        if(ObjectUtil.isString(title)) {
            var titleNode = g.select("title");
            titleNode.node() ? titleNode.text(title) : g.append("title").text(title);
        }
    	var background = self.config().background;
    	if(backgroundEle && background) {
    		background.color = RegexUtil.replace(RegexUtil.dataRegex,background.color,self.data());
            backgroundEle.data(background);
        }
        self.resetBBox(g);
    }
    //override
    this.drawPlugin = function(g){
        var config = ObjectUtil.cloneObj(self.config());
        config.color = RegexUtil.replace(RegexUtil.dataRegex,config.color,self.data());
        config.url = RegexUtil.replace(RegexUtil.dataRegex,config.url,self.data());
        config.imageWidth = RegexUtil.replace(RegexUtil.dataRegex,config.imageWidth,self.data());
        config.imageHeight = RegexUtil.replace(RegexUtil.dataRegex,config.imageHeight,self.data());
        var title = RegexUtil.replace(RegexUtil.dataRegex,config.title,self.data());
        if(ObjectUtil.isString(title)) {
        	var titleNode = g.select("title");
        	titleNode.node() ? titleNode.text(title) : g.append("title").text(title);
        }
        var background = config.background;
        if(background) {
        	background.color = RegexUtil.replace(RegexUtil.dataRegex,background.color,self.data());
        	backgroundEle = new RectElement(g, background);
            backgroundEle.render();
        }
        imageElement = new ImageElement(g,config);
        imageElement.render();
        
        self.resetBBox(g);
    };
};

JiguiNode.struct = function(room2d) {
	var Plugin = ghca_charts.view.elements.plugins.plugin;
	Plugin.struct(ghca_charts);
    JiguiNode.prototype = Object.create(Plugin.prototype);
    JiguiNode.prototype.constructor = JiguiNode;
};

module.exports = JiguiNode;
